package cn.dnui_zjs008.dnuifood_.model;

import cn.dnui_zjs008.dnuifood_.bean.UserBean;
import cn.dnui_zjs008.dnuifood_.iface.UserIface;
import cn.dnui_zjs008.dnuifood_.listener.OnRetrofitListener;
import cn.dnui_zjs008.dnuifood_.service.UserService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class UserModel implements UserIface {
    private Retrofit retrofit;
    private UserService service;
    private String BaseUrl="http://172.24.10.175:8080/foodService/";
    public UserModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl(BaseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service = retrofit.create(UserService.class);
    }
    @Override
    public void login(String username, String userpass, final OnRetrofitListener listener) {
        Call<UserBean> call = service.doLogin(username,userpass);
        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                if(response.body()!=null)
                    listener.onSuccess(response.body());
                else
                    listener.onFailure("解析有误！");
            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
                listener.onFailure("联网异常！");
            }
        });
    }

    @Override
    public void reg(String username, String userpass, String mobilenum, String address, String comment, OnRetrofitListener listener) {

    }
}
